package com.cabrida.livestar.main.me;

import org.apache.http.Header;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.main.MainActivity;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.cabrida.livestar.util.SharedUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 修改名字
 * 
 * @author why
 */
public class PersonalChangeNameActivity extends BaseActivity {
	public static final String TAG = PersonalChangeNameActivity.class.getSimpleName();
	private ImageView iv_back;
	private TextView tv_title;
	private TextView tv_right;
	private EditText et_name;
	private String str_nickname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_personal_changename);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_right = (TextView) findViewById(R.id.tv_right);
		et_name = (EditText) findViewById(R.id.et_name);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
		tv_right.setOnClickListener(this);
		tv_title.setText(getResources().getString(R.string.fans_change_information_pensonal));
		tv_right.setText(getResources().getString(R.string.finish));
		et_name.setHint(Global.U_nickname);
	}
	
	private void getNameData() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("nickname", str_nickname);

		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_PERSONAL_EDIT_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getNameData", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);

						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
							Global.U_nickname = str_nickname;
							SharedUtils.putString(context, "U_nickname", Global.U_nickname);
							Intent intent = new Intent();
							intent.setAction(MainActivity.ACTION_EDIT_INFORMATION);
							context.sendBroadcast(intent);
							finish();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.tv_right:
			str_nickname = et_name.getText().toString().trim();
			if (TextUtils.isEmpty(str_nickname)) {
				showToast("昵称不能为空！");
				break;
			}
			getNameData();
			break;

		default:
			break;
		}
	}
	


}
